﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DBUtility;//Please add references
namespace DAL
{
	/// <summary>
	/// 数据访问类:Volunteer_Apply
	/// </summary>
	public partial class Volunteer_Apply
	{
		public Volunteer_Apply()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("Id", "Volunteer_Apply"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Volunteer_Apply");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Model.Volunteer_Apply model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Volunteer_Apply(");
			strSql.Append("Email,Name,Age,Sex,PhoneNumber,QQ,DistrictId,Slogan,IsReject,InsTime,IsPass,Remark,DisposeTime)");
			strSql.Append(" values (");
			strSql.Append("@Email,@Name,@Age,@Sex,@PhoneNumber,@QQ,@DistrictId,@Slogan,@IsReject,@InsTime,@IsPass,@Remark,@DisposeTime)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@Age", SqlDbType.Int,4),
					new SqlParameter("@Sex", SqlDbType.NVarChar,5),
					new SqlParameter("@PhoneNumber", SqlDbType.NVarChar,50),
					new SqlParameter("@QQ", SqlDbType.NVarChar,50),
					new SqlParameter("@DistrictId", SqlDbType.Int,4),
					new SqlParameter("@Slogan", SqlDbType.NVarChar),
					new SqlParameter("@IsReject", SqlDbType.Bit,1),
					new SqlParameter("@InsTime", SqlDbType.DateTime),
					new SqlParameter("@IsPass", SqlDbType.Bit,1),
					new SqlParameter("@Remark", SqlDbType.NVarChar,300),
					new SqlParameter("@DisposeTime", SqlDbType.DateTime)};
			parameters[0].Value = model.Email;
			parameters[1].Value = model.Name;
			parameters[2].Value = model.Age;
			parameters[3].Value = model.Sex;
			parameters[4].Value = model.PhoneNumber;
			parameters[5].Value = model.QQ;
			parameters[6].Value = model.DistrictId;
			parameters[7].Value = model.Slogan;
			parameters[8].Value = model.IsReject;
			parameters[9].Value = model.InsTime;
			parameters[10].Value = model.IsPass;
			parameters[11].Value = model.Remark;
			parameters[12].Value = model.DisposeTime;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Model.Volunteer_Apply model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Volunteer_Apply set ");
			strSql.Append("Email=@Email,");
			strSql.Append("Name=@Name,");
			strSql.Append("Age=@Age,");
			strSql.Append("Sex=@Sex,");
			strSql.Append("PhoneNumber=@PhoneNumber,");
			strSql.Append("QQ=@QQ,");
			strSql.Append("DistrictId=@DistrictId,");
			strSql.Append("Slogan=@Slogan,");
			strSql.Append("IsReject=@IsReject,");
			strSql.Append("InsTime=@InsTime,");
			strSql.Append("IsPass=@IsPass,");
			strSql.Append("Remark=@Remark,");
			strSql.Append("DisposeTime=@DisposeTime");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@Age", SqlDbType.Int,4),
					new SqlParameter("@Sex", SqlDbType.NVarChar,5),
					new SqlParameter("@PhoneNumber", SqlDbType.NVarChar,50),
					new SqlParameter("@QQ", SqlDbType.NVarChar,50),
					new SqlParameter("@DistrictId", SqlDbType.Int,4),
					new SqlParameter("@Slogan", SqlDbType.NVarChar),
					new SqlParameter("@IsReject", SqlDbType.Bit,1),
					new SqlParameter("@InsTime", SqlDbType.DateTime),
					new SqlParameter("@IsPass", SqlDbType.Bit,1),
					new SqlParameter("@Remark", SqlDbType.NVarChar,300),
					new SqlParameter("@DisposeTime", SqlDbType.DateTime),
					new SqlParameter("@Id", SqlDbType.Int,4)};
			parameters[0].Value = model.Email;
			parameters[1].Value = model.Name;
			parameters[2].Value = model.Age;
			parameters[3].Value = model.Sex;
			parameters[4].Value = model.PhoneNumber;
			parameters[5].Value = model.QQ;
			parameters[6].Value = model.DistrictId;
			parameters[7].Value = model.Slogan;
			parameters[8].Value = model.IsReject;
			parameters[9].Value = model.InsTime;
			parameters[10].Value = model.IsPass;
			parameters[11].Value = model.Remark;
			parameters[12].Value = model.DisposeTime;
			parameters[13].Value = model.Id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Volunteer_Apply ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Volunteer_Apply ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.Volunteer_Apply GetModel(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 Id,Email,Name,Age,Sex,PhoneNumber,QQ,DistrictId,Slogan,IsReject,InsTime,IsPass,Remark,DisposeTime from Volunteer_Apply ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			Model.Volunteer_Apply model=new Model.Volunteer_Apply();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["Id"]!=null && ds.Tables[0].Rows[0]["Id"].ToString()!="")
				{
					model.Id=int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Email"]!=null && ds.Tables[0].Rows[0]["Email"].ToString()!="")
				{
					model.Email=ds.Tables[0].Rows[0]["Email"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Name"]!=null && ds.Tables[0].Rows[0]["Name"].ToString()!="")
				{
					model.Name=ds.Tables[0].Rows[0]["Name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Age"]!=null && ds.Tables[0].Rows[0]["Age"].ToString()!="")
				{
					model.Age=int.Parse(ds.Tables[0].Rows[0]["Age"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Sex"]!=null && ds.Tables[0].Rows[0]["Sex"].ToString()!="")
				{
					model.Sex=ds.Tables[0].Rows[0]["Sex"].ToString();
				}
				if(ds.Tables[0].Rows[0]["PhoneNumber"]!=null && ds.Tables[0].Rows[0]["PhoneNumber"].ToString()!="")
				{
					model.PhoneNumber=ds.Tables[0].Rows[0]["PhoneNumber"].ToString();
				}
				if(ds.Tables[0].Rows[0]["QQ"]!=null && ds.Tables[0].Rows[0]["QQ"].ToString()!="")
				{
					model.QQ=ds.Tables[0].Rows[0]["QQ"].ToString();
				}
				if(ds.Tables[0].Rows[0]["DistrictId"]!=null && ds.Tables[0].Rows[0]["DistrictId"].ToString()!="")
				{
					model.DistrictId=int.Parse(ds.Tables[0].Rows[0]["DistrictId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Slogan"]!=null && ds.Tables[0].Rows[0]["Slogan"].ToString()!="")
				{
					model.Slogan=ds.Tables[0].Rows[0]["Slogan"].ToString();
				}
				if(ds.Tables[0].Rows[0]["IsReject"]!=null && ds.Tables[0].Rows[0]["IsReject"].ToString()!="")
				{
					if((ds.Tables[0].Rows[0]["IsReject"].ToString()=="1")||(ds.Tables[0].Rows[0]["IsReject"].ToString().ToLower()=="true"))
					{
						model.IsReject=true;
					}
					else
					{
						model.IsReject=false;
					}
				}
				if(ds.Tables[0].Rows[0]["InsTime"]!=null && ds.Tables[0].Rows[0]["InsTime"].ToString()!="")
				{
					model.InsTime=DateTime.Parse(ds.Tables[0].Rows[0]["InsTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["IsPass"]!=null && ds.Tables[0].Rows[0]["IsPass"].ToString()!="")
				{
					if((ds.Tables[0].Rows[0]["IsPass"].ToString()=="1")||(ds.Tables[0].Rows[0]["IsPass"].ToString().ToLower()=="true"))
					{
						model.IsPass=true;
					}
					else
					{
						model.IsPass=false;
					}
				}
				if(ds.Tables[0].Rows[0]["Remark"]!=null && ds.Tables[0].Rows[0]["Remark"].ToString()!="")
				{
					model.Remark=ds.Tables[0].Rows[0]["Remark"].ToString();
				}
				if(ds.Tables[0].Rows[0]["DisposeTime"]!=null && ds.Tables[0].Rows[0]["DisposeTime"].ToString()!="")
				{
					model.DisposeTime=DateTime.Parse(ds.Tables[0].Rows[0]["DisposeTime"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Id,Email,Name,Age,Sex,PhoneNumber,QQ,DistrictId,Slogan,IsReject,InsTime,IsPass,Remark,DisposeTime ");
			strSql.Append(" FROM Volunteer_Apply ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" Id,Email,Name,Age,Sex,PhoneNumber,QQ,DistrictId,Slogan,IsReject,InsTime,IsPass,Remark,DisposeTime ");
			strSql.Append(" FROM Volunteer_Apply ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Volunteer_Apply ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.Id desc");
			}
			strSql.Append(")AS Row, T.*  from Volunteer_Apply T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Volunteer_Apply";
			parameters[1].Value = "Id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

